home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 0195.ZIP / FANCYKEY.PAS < prev    next >
Pascal/Delphi Source File  |  1984-12-20  |  4KB  |  120 lines

  1. {@@@@@@@@@@@ copyright (C) 1984 by Neil J. Rubenking @@@@@@@@@@@@@@@@@@@@@@@@
  2. The purchaser of these procedures and functions may include them in COMPILED
  3. programs freely, but may not sell or give away the source text.
  4.  
  5.     This is a fancy driver for KEYBOARD.LIB.  It returns a character
  6.     or descriptive phrase for ANY key pressed.  Note, though, that the
  7.     <Alt> + <keypad number> combinations will be rendered as their
  8.     equivalent <Ctrl> + <letter>s. (Alt-1 = Ctrl-A, etc.)
  9.  
  10.  
  11. }
  12.  
  13.  
  14. {$I regpack.typ}
  15. {$I keyboard.lib}
  16. type
  17.   KeyType = string[12];
  18. var
  19.   KeyValue            : integer;
  20.   ASCIIcode, ScanCode : byte;
  21.   Result              : KeyType;
  22.  
  23. {@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@}
  24. function Read_Keyboard: KeyType;
  25. var
  26.   TempRead : KeyType;
  27.   {============================================================}
  28. function SpecialKey(Code:byte):KeyType;
  29. const
  30.   Row0 : KeyType = '1234567890-=';
  31.   Row1 : KeyType = 'QWERTYUIOP';
  32.   Row2 : KeyType = 'ASDFGHJKL';
  33.   Row3 : KeyType = 'ZXCVBNM';
  34. var
  35.   temp : KeyType;
  36. begin
  37. case code of
  38.        14: temp := 'BackSpace';
  39.        15: temp := 'Back Tab';
  40.    16..25: temp := 'Alt-' + Row1[code-15];
  41.    30..38: temp := 'Alt-' + Row2[code-29];
  42.    44..50: temp := 'Alt-' + Row3[code-43];
  43.  120..131: temp := 'Alt-' + Row0[code-119];
  44.    59..67: temp := 'F' + chr(code - 10);
  45.        68: temp := 'F10';
  46.    84..92: temp := 'Shift F' + chr(code-35);
  47.        93: temp := 'Shift F10';
  48.   94..102: temp := 'Ctrl-F' + chr(code-45);
  49.       103: temp := 'Ctrl-F10';
  50.  104..112: temp := 'Alt-F' + chr(code-55);
  51.       113: temp := 'Alt-F10';
  52.        71: temp := 'Home';
  53.        72: temp := 'Up';
  54.        73: temp := 'PgUp';
  55.        75: temp := 'Left';
  56.        77: temp := 'Right';
  57.        79: temp := 'End';
  58.        80: temp := 'Down';
  59.        81: temp := 'PgDn';
  60.        82: temp := 'Ins';
  61.        83: temp := 'Del';
  62.       114: temp := 'Ctrl-PrtSc';
  63.       115: temp := 'Ctrl-Left';
  64.       116: temp := 'Ctrl-Right';
  65.       117: temp := 'Ctrl-End';
  66.       118: temp := 'Ctrl-PgDn';
  67.       119: temp := 'Ctrl-Home';
  68.       132: temp := 'Ctrl-PgUp';
  69.  else
  70.    temp := 'Ctrl-Break';
  71.  end;  {case}
  72. SpecialKey := temp;
  73. end;
  74.   {============================================================}
  75. function SpecChr(code:byte):KeyType;
  76. begin
  77.   case code of
  78.      0..26 : SpecChr := 'Ctrl-' + chr(code + 64);
  79.         27 : SpecChr := 'Esc';
  80.    28..255 : SpecChr := chr(code);
  81.   end;
  82. end;
  83.   {============================================================}
  84. begin
  85.     KeyValue  := KeyBoard('W');
  86.     ScanCode  := KeyValue shr 8;
  87.     ASCIICode := (KeyValue shl 8) shr 8;
  88.                     {The special keys that have no ASCII character generate }
  89.                     {a zero in place of the code.  However, there are three }
  90.                     {non-printable characters that DO have an ASCII code.   }
  91.                     {Their scan codes are 14, 15, and 28.  We provide for   }
  92.                     {them below.                                            }
  93.  
  94.     if (not (ScanCode in [14,15,28])) and (ASCIICode <> 0)  then
  95.       TempRead := SpecChr(ASCIICode)
  96.     else
  97.       begin
  98.         if ASCIICode <> 0 then
  99.           begin
  100.             case ScanCode of
  101.               14: TempRead := 'BackSpace';
  102.               15: TempRead := 'Tab';
  103.               28: TempRead := 'Return';
  104.             end;  {case}
  105.           end  {second if}
  106.       else
  107.     TempRead := SpecialKey(ScanCode);
  108.       end;  {the upper else}
  109.    Read_Keyboard := TempRead;
  110. end;
  111.  
  112. {@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@}
  113. begin
  114.   WriteLn('Press various keys and key combinations.');
  115.   WriteLn('Press <ctrl><break> to stop.');
  116.   repeat
  117.     Result := Read_Keyboard;
  118.     WriteLn(Result);
  119.   until Result = 'Ctrl-Break';
  120. end.